package caltest;

public class CalTest {
    //输入：{1,3,5},{2,4,6}
    //返回值：{1,2,3,4,5,6}
    public static void main(String[] args) {
//        System.err.println("result = " + test(new int[]{1,3,5}, new int[]{2,4,6}));

        int[] result = test(new int[]{1}, new int[]{1});

        System.err.print("[");
        for (int i = 0; i < result.length; i++) {
            if (i < result.length - 1) {
                System.err.print(result[i] + ", ");
            } else {
                System.err.print(result[i]);
            }
        }
        System.err.println("]");
    }

    public static int[] test(int[] a, int[] b) {
        int[] r = new int[a.length + b.length];
        int ap = 0;
        int bp = 0;

        for (int i = 0; i < r.length; i++) {
            if (ap < a.length && bp < b.length) {
                // 都不越界
                if (a[ap] <= b[bp]) {
                    r[i] = a[ap];
                    ap++;
                } else {
                    r[i] = b[bp];
                    bp++;
                }
            } else if (ap < a.length) {
                // 只有a没有越界
                r[i] = a[ap];
                ap++;
            } else {
                // 只有b没有越界
                r[i] = b[bp];
                bp++;
            }
        }

        return r;
    }
}
